Infolinks In Text Ads

Wednesday 17 August 2011

First network programming example


First network programming example
 There is a server in Boulder, Colorado,  that provides the exact time of day (in Greenwich Mean Time) upon request.   You can contact that server via the Telnet utility (from a command line) to see what output it provides.  The 13 on the command line is the port number at which the service is provided.  Notice that the command line provides a domain name and port number, just what is needed to construct a remote endpoint.
What is not quite obvious from this printout (until I point it out) is that the output from the server begins and ends with a newline. The output specifies today’s date and time.  It doesn’t matter for our purposes what the rest of the output is.   NIST is the National Institute of Standards,  which maintains this server.
 What we want to do is write our own low-level network program to establish a socket connection to the NIST.    Here are the steps to do this:
 1.   Make a new  C# Windows Application  called NetworkDemo.   Drag onto your form a button (label it Get Time)  and a multi-line list box to receive the output.   The idea is that when you click the button,  a connection will be established to the NIST server and the output will be displayed in the list box.
 2.   All the interesting code will be in the GetTimeButton_Click handler.  Here it is:
 IPHostEntry resolvedServer;
IPEndPoint serverEndPoint;
Socket tcpSocket = null;
try
{ resolvedServer =
     Dns.GetHostEntry(“time-A.timefreq.bldrdoc.gov”);
     // here is where we use DNS to get an IP adress
     // Actually,  an IPHostEntry can contain a whole
     // list of possible IP addresses for the domain.
  foreach (IPAddress addr in resolvedServer.AddressList)
  {
    tcpSocket = new Socket(
            addr.AddressFamily,
            SocketType.Stream,
            ProtocolType.Tcp);
    serverEndPoint = new IPEndPoint(addr, 13);
    /* the time of day service is on port 13 */
    try
       {
         tcpSocket.Connect(serverEndPoint);
       }
    catch
       { // connect failed so try the next one
         if (tcpSocket != null)
             tcpSocket.Close();
         continue;
       }
    break;  // successful connection
  }
}
catch (SocketException err)
{
  textBox1.Text = ”Client connection failed” + err.Message;
}
// now tcpSocket is open, use it
byte[] receiveBuffer = new byte[1024];
int nReceived;
String sReceived;
try
{
  textBox1.Clear();
  textBox1.Text = ”";
  nReceived = tcpSocket.Receive(receiveBuffer);
  sReceived =
   Encoding.UTF8.GetString(receiveBuffer).Substring(1,49);
  // we don’t want the initial and final newlines
  textBox1.Text = sReceived.Trim();
  // for some reason Trim doesn’t get rid of the final
  // newline character as the documentation says it should.
  /* The following low-level code also works:
     for (int i = 0; i < nReceived; i++)
       if(receiveBuffer[i] != ‘\n’)
          textBox1.Text += (char) receiveBuffer[i];
       if(receiveBuffer[i] == ”)
           break;
  */
}
catch (SocketException)
{
  textBox1.Text = ”Some unforeseen error occurred”;
}
}

Twitter Delicious Facebook Digg Stumbleupon Favorites More